package com.sde.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.sde.Enum.ResponseCodeEnum;
import com.sde.exception.BusinessException;
import com.sde.mapper.UserMapper;
import com.sde.pojo.User;
import com.sde.pojo.dto.UserInfoDto;
import com.sde.service.UserService;
import com.sde.utils.Md5Util;
import com.sde.utils.ThreadLocalUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;



    /**
     * 根据用户名查询用户
     * @param name
     * @return
     */
    @Override
    public User getUserByName(String name) {
        User user = userMapper.selectUserByName(name);
        if(ObjectUtil.isNotNull(user)){
            return user;
        }
        return null;
    }

    /**
     * 注册用户
     * @param userName
     * @param password
     */
    @Override
    public void register(String userName, String password) {
        //1，先判断用户名是否已经存在
        User user = getUserByName(userName);
        if (ObjectUtil.isNotNull(user)){
            throw new BusinessException(ResponseCodeEnum.USER_IS_EXISTS);
        }

        //2，密码进行MD5加密
        String md5Pwd = Md5Util.getMD5String(password);

        //3，补充参数
        User newUser = User.builder()
                .username(userName)
                .password(md5Pwd)
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .build();

        //4，向后端发送添加的请求
        userMapper.insertUser(newUser);
    }

    /**
     * 用户登录
     * @param userName
     * @param password
     * @return
     */
    @Override
    public User login(String userName, String password) {
        // 1，先查询用户是否存在
        User user = userMapper.selectUserByName(userName);
        if (ObjectUtil.isNull(user)){
            throw new BusinessException(ResponseCodeEnum.USER_IS_NOT_EXISTS);
        }
        //2，比对用户输入的密码和数据库里面的密码
        boolean flag = Md5Util.checkPassword(password, user.getPassword());
        if (!flag){
            throw new BusinessException(ResponseCodeEnum.PASSWORD_ERROR);
        }

        return user;
    }

    /**
     * 获取用户信息
     * @param userId
     * @return
     */
    @Override
    public User getUserInfo(Integer userId) {
        //1，调用mapper接口
        User user = userMapper.selectUserById(userId);
        //2，判断用户是否存在
        if (ObjectUtil.isNull(user)){
            throw new BusinessException(ResponseCodeEnum.USER_IS_NOT_EXISTS);
        }
        return user;
    }

    /**
     * 更新用户信息
     * @param userInfoDto
     */
    @Override
    public void updateUserInfo(UserInfoDto userInfoDto) {
        //1，属性拷贝到user对象中
        User user = new User();
        user.setUpdateTime(LocalDateTime.now());
        BeanUtils.copyProperties(userInfoDto,user);
        //2，调用mapper接口
        userMapper.update(user);
    }

    /**
     * 更新用户头像
     * @param avatarUrl
     */
    @Override
    public void updateAvatar(String avatarUrl) {
        //1，在ThreadLocal中获取当前用户的id
        Integer userId = ThreadLocalUtil.get();
        //2，调用mapper接口修改
        userMapper.updateAvator(avatarUrl,userId);
    }

    /**
     * 更新用户密码
     * @param userInfoDto
     */
    @Override
    public void updatePwd(UserInfoDto userInfoDto) {
        //1，先判断旧密码是否正确
        User user = userMapper.selectUserByPwd(userInfoDto.getOldPwd());
        //2，判断user对象是否为空
        if (ObjectUtil.isNull(user)){
            throw new BusinessException(ResponseCodeEnum.USER_IS_NOT_EXISTS);
        }
        //3，判断新密码是否未空
        if (StrUtil.isEmpty(userInfoDto.getNewPwd()) || StrUtil.isEmpty(userInfoDto.getRePwd())){
            throw new BusinessException(ResponseCodeEnum.PWD_AND_REPWD_IS_NULL);
        }
        //4，判断两个密码是否相同

        //5，修改密码
    }
}
